{% extends 'base_backend.html' %}
{% load static %}
{% load my_filter %}

{% block header_tail %}
<link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/style.css' %}">
<link rel="stylesheet" href="{% static 'zhongbao/zhongbao_admin/css/iconfont.css' %}">
<link rel="stylesheet" href="{% static 'plugins/datatables/dataTables.bootstrap.css' %}">
<link rel="stylesheet" href="{% static 'public/ComboSelect/css/combo.select.css' %}">
<link rel="stylesheet" href="{% static 'zhongbao/config/css/main.css' %}">
<link rel="stylesheet" href="{% static 'laydate/theme/default/laydate.css' %}">
<script src="{% static 'laydate/laydate.js' %}"></script>
<script src="{% static 'plugins\jQuery\jquery-2.2.3.min.js' %}"></script>

<script src="{% static 'plugins/vue1/vue.js' %}"></script>

{% endblock header_tail %}

{% block section_content %}

<div>
    <div class="box-header with-border">
        <h3 class="box-title">
            错误反馈
            {% if user.is_superuser %}
            <button class="btn btn-primary" data-toggle="modal" data-target="#errFeedbackMessage">上传文件</button>
            <button class="btn btn-primary" data-toggle="modal" data-target="#downloadFeedbackMessage">下载文件</button>
            {% endif %}
        </h3>
    </div>
    <!-- form start -->
    <div class="box box-primary">
        <div class="box-header with-border" style="border-color: #1b6d85">
            <!-- Date range -->
            <form role="form" id="searchForm" method="get" action="">
                <input name="author" type="hidden" class="config-input" value="{{ author }}">
                <div style="width: 95%; float: left;">
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="taskId" class="control-label">任务ID：</label>
                        <p>
                            <input id="taskId" name="task_id" type="text" class="config-input">
                        </p>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="username" class="control-label">用户名：</label>
                        <input id="username" type="text" name="username" class="config-input">
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="savename" class="control-label">文件名：</label>
                        <input id="savename" type="text" name="savename" class="config-input">
                    </div>
                    {% if user.is_superuser %}
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="agent_name" class="control-label">代理名称：</label>
                        <input id="agent_name" type="text" name="agent_name" class="config-input">
                    </div>
                    {% endif %}
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">任务状态：</label>
                        <select name="status" id="taskStatus" class="config-input">
                            {% for filter in filters %}
                            {% if filter.code == status %}
                            <option selected="selected" value="{{ filter.code }}">{{ filter.description }}</option>
                            {% else %}
                            <option value="{{ filter.code }}">{{ filter.description }}</option>
                            {% endif %}
                            {% endfor %}
                        </select>
                    </div>
                    <div style="float: left;" class="input-group select-config-div">
                        <label for="id" class="control-label">每页显示：</label>
                        <select name="limit" id="showNumSelect" class="config-input">
                            {% for i in limits %}
                            {% if i == limit %}
                            <option selected="selected" value="{{ i }}">{{ i }}条</option>
                            {% else %}
                            <option value="{{ i }}">{{ i }}条</option>
                            {% endif %}
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="input-group" style="width: 5%; float: right; margin-top: 70px;">
                    <input type="submit" class="form-control pull-left btn btn-sm btn-primary" value="检索">
                </div>
            </form>
        </div>
        <!-- 展示搜索出来的任务 -->
        <div id="main">
            <div class="box-body">
                <form id="model_form" role="form" method="post">{% csrf_token %}
                    <div class="group">
                        <table id="task_each_table" border="1" class="table table-bordered table-hover table-striped">
                            <thead>
                            <tr>
                                <th>
                                    <span style="float: left">
                                        任务ID
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        网站名称
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        文件名
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        测试链接
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        错误信息
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        备注
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        测试时间
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        批次
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        状态
                                    </span>
                                </th>
                                <th>
                                    <span style="float: left">
                                        用户名
                                    </span>
                                </th>
                                {% if user.is_superuser %}
                                <th>
                                    <span style="float: left">
                                        代理名称
                                    </span>
                                </th>
                                {% endif %}
                                <th>
                                    <span style="float: left">
                                        操作
                                    </span>
                                </th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for task in tasks %}
                            <tr>
                                <td>{{ task.task_id }}</td>
                                <td title="{{ task.webname }}">{{ task.webname|truncatechars_html:15 }}</td>
                                <td title="{{ task.savename }}">{{ task.savename|truncatechars_html:15 }}</td>
                                <td><a href="{{ task.test_url }}" title="{{ task.test_url }}">
                                    {{ task.test_url|truncatechars_html:30 }}</a></td>
                                <td title="{{ task.unfeedback }}">{{ task.unfeedback|truncatechars_html:30 }}</td>
                                <td title="{{ task.remark }}">{{ task.remark|truncatechars_html:30 }}</td>
                                <td>{{ task.testdate }}</td>
                                <td>{{ task.batch }}</td>
                                <td>{{ task.status|get_status }}</td>
                                <td>{{ task.uid|get_username }}</td>
                                {% if user.is_superuser %}
                                <td>{{ task.agent_id|get_agent_name }}</td>
                                {% endif %}
                                <td>
                                    {% if user.is_superuser and author == 'admin' and task.status == 1 %}
                                    <button class="btn btn-info btn-xs" onclick="syncUpdate('{{ task.task_id }}')">
                                        同步修改
                                    </button>
                                    {% endif %}
                                    {% if author == 'admin' or author == 'agent' %}
                                    <a href="{% url 'task:detail' %}?id={{ task.task_id }}&type=task&user=admin"
                                       class="btn btn-info btn-xs">详情</a>
                                    {% else %}
                                    <a href="{% url 'task:detail' %}?id={{ task.task_id }}&type=task&user=user"
                                       class="btn btn-info btn-xs">详情</a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                        <p class="" id="paginator">
                            <a class="btn btn-default btn-xs" onclick="toPage('home')">首页</a>
                            <a class="btn btn-default btn-xs" onclick="toPage('last')">上一页</a>
                            <a class="btn btn-default btn-xs" onclick="toPage('next')">下一页</a>
                            <input type="text" id="page-judge" style="width: 30px;height:20px; margin-left: 20px"
                                   onkeyup="toPage('judge')">
                            <a class="btn btn-default btn-xs" onclick="toPage('next')">尾页</a>
                            <a class="btn btn-default btn-xs" onclick="toPage('judge')">跳页</a>
                            <span v-show="total">共{{ count }}个</span>
                        </p>
                    </div>
                    <!-- /.box-footer -->
                    <div class="box-footer">
                        <button class="btn btn-primary no-display" id="submit_button">确认</button>
                        <button type="button" class="btn btn-primary" id="save_submit" name="_save"
                                style="display: none">sure
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--上传错误信息文件-->
<div class="modal fade" id="errFeedbackMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     style="top:40%">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div id="errFeedbackBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                <form role="form" method="post" action="" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="form-group">
                        <label>错误信息上传:</label>
                        <div class="input-group date">
                            <input name="err_file" type="file" class="config_file">
                        </div>
                    </div>
                    <input type="submit" class="btn btn-primary" value="确定">
                </form>
            </div>
            <div class="modal-footer" style="text-align:center;">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">取消
                </button>
            </div>
        </div>
    </div>
</div>
<!--导出记录-->
<div class="modal fade" id="downloadFeedbackMessage" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     style="top:40%">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div id="downloadFeedbackBoxMsg" class="modal-body" style="overflow:auto;text-align:center;">
                <form style="margin-top: 10px;" role="form" method="get" action="{% url 'task:download_feedback' %}">
                    {% csrf_token %}
                    <div class="form-group">
                        <label>反馈记录导出:</label>
                        <div class="input-group date">
                            <div class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                            </div>
                            <input name="testTime_range" type="text" class="form-control pull-right" autocomplete="off"
                                   id="testTime_range">
                        </div>
                    </div>
                    <input type="submit" class="btn btn-primary" value="确定">
                </form>
            </div>
            <div class="modal-footer" style="text-align:center;">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">取消
                </button>
            </div>
        </div>
    </div>
</div>

<script>
    function changeColor(id, color) {
        //$(id).parent().parent().parent().children().children().children().find("li[color='green']");
        $(id).parent().parent().addClass("active");
        $(id).children().css("color", color);
    }

    if ('{{ author }}' == 'admin') {
        changeColor('#err_feedback', '#00FF7F');
    } else if ('{{ author }}' == 'agent') {
        changeColor('#err_config_agent', '#00FF7F');
    } else {
        changeColor('#err_config', '#00FF7F');
    }

    // 获取当前url参数值
    function GetQueryValue1(queryName) {
        var reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURI(r[2]);
        } else {
            return null;
        }
    }

    // 翻页
    function toPage(type) {
        location_url = window.location.href;
        var page_num = GetQueryValue1('page');
        if (!page_num) {
            page_num = 1
        }
        if (type === 'last') {
            if (Number(page_num) > 1) {
                page_num = Number(page_num) - 1
            } else {
                page_num = 1
            }
        } else if (type === 'next') {
            page_num = Number(page_num) + 1
        } else if (type === 'judge') {
            page_num = $("#page-judge").val()
        } else if (type === 'home') {
            page_num = 1
        } else {
            alert('参数错误');
            return false;
        }
        var page = 'page=' + page_num;
        if (location_url.indexOf('page') !== -1) {
            window.location.href = location_url.replace(/page=\d+/, page)
        } else {
            if (location_url.indexOf('?') === -1) {
                window.location.href = location_url + '?' + page
            } else {
                window.location.href = location_url + '&' + page;
            }
        }
    }

    function syncUpdate(task_id) {
        $.post({
            url: "{% url 'task:syncUpdate' %}",
            data: {
                csrfmiddlewaretoken: '{{ csrf_token }}',
                task_id: task_id
            },
            success: function (resp) {
                alert(resp.message);
                window.location.reload()
            }
        })
    }

    laydate.render({
        elem: '#testTime_range'
        , range: true
    });
</script>
{% endblock section_content %}
